<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'models/m_object.php';

/**
 * Модель меню
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class M_Menu extends M_Object {
   
   /**
    * Название таблицы
    *
    * @access protected
    * @var string
    */
   var $_table = 'menus';
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return M_Menu
    */
   function M_Menu() {
      parent::M_Object();
   } // end M_Menu
   
   /**
    * Метод активации меню
    *
    * @access public
    * @return boolean
    */
   function activate() {
      $sql = "
         UPDATE
            " . $this->db->dbprefix($this->_table) . "
         SET
            status = 'active'
         WHERE
            id_menu = " . $this->db->escape($this->get_id('id_menu')) . " AND
            status <> 'active'
         LIMIT
            1
      ";
      if (null !== $this->db->query($sql)) {
         return true;
      }
      return false;
   } // end activate
   
   /**
    * Метод приостановки меню
    *
    * @access public
    * @return boolean
    */
   function pause() {
      $sql = "
         UPDATE
            " . $this->db->dbprefix($this->_table) . "
         SET
            status = 'paused'
         WHERE
            id_menu = " . $this->db->escape($this->get_id('id_menu')) . " AND
            status <> 'paused'
         LIMIT
            1
      ";
      if (null !== $this->db->query($sql)) {
         return true;
      }
      return false;
   } // end pause
   
   /**
    * Удаление записи
    *
    * @access protected
    */
   function _delete() {
      $sql = "
         UPDATE
            " . $this->db->dbprefix($this->_table) . "
         SET
            status = 'deleted'
         WHERE
            id_menu = " . $this->db->escape($this->get_id('id_menu')) . " AND
            status <> 'deleted'
         LIMIT
            1
      ";
      if (null !== $this->db->query($sql)) {
         return true;
      }
      return false;
   } // end _delete
   
   /**
    * Удаление содержимого меню
    *
    * @access public
    * @return boolean
    */
   function delete_contents() {
      $sql = "
         DELETE FROM
            " . $this->db->dbprefix('contents') . "
         WHERE
            id_menu = " . $this->db->escape($this->get_id('id_menu')) . "
      ";
      if (null !== $this->db->query($sql)) {
         return true;
      }
      return false;
   } // end delete_contents
   
   /**
    * Получение идентификатора меню по названию
    *
    * @param string $name
    * @return int
    */
   function get_id_by_name($name) {
      $id_menu = 0;
      $this->db->select('id_menu')
         ->from('menus')
         ->where('name', $name)
         ->limit(1);
      $query = $this->db->get();
      if (0 < $query->num_rows()) {
         $row = $query->row_array();
         $id_menu = $row['id_menu'];
      }
      return $id_menu;
   }
   
}
